﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace FPC.Offer.UI.GeneralProcessor.SkyDriveHand
{
    /// <summary>
    /// DownloadZiHandler 的摘要说明
    /// </summary>
    public class DownloadZiHandler : IHttpHandler
    {
        FPC.Offer.Service.System_SubfolderTableService sky = new Service.System_SubfolderTableService();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var id = Convert.ToInt32(context.Request["id"]);
            Model.Subfolder c = sky.Find(w => w.SId == id).FirstOrDefault();
            var Surl = c.SUrl;
            var Name = c.SName;
            //    context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Name, Encoding.UTF8));
            //    context.Response.WriteFile(Surl);
            //    context.Response.Write("1");
            string fileName = Name;//客户端保存的文件名
            string filePath = Surl;

            FileInfo fileInfo = new FileInfo(filePath);
            context.Response.Clear();
            context.Response.ClearContent();
            context.Response.ClearHeaders();
            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            context.Response.AddHeader("Content-Transfer-Encoding", "binary");
            context.Response.ContentType = "application/octet-stream";
            context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            context.Response.WriteFile(fileInfo.FullName);
            context.Response.Flush();
            context.Response.End();



        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}